{createAction} = require('redux-actions')

actions =
  getListStart:createAction('OUTPLAN/GET_LIST_START')
  getListCompleted:createAction('OUTPLAN/GET_LIST_COMPLETED')
  refreshListStart:createAction('OUTPLAN/REFRESH_LIST_START')
  refreshListCompleted:createAction('OUTPLAN/REFRESH_LIST_COMPLETED')
  fetchRefreshList:(state,token)->
    (dispatch)->
      dispatch actions.refreshListStart()
      fetch("http://202.136.214.173/api/V_SHORT_OUTPLAN?state=#{state}",{
          method: 'GET'
          mode: 'cors'
          cache: 'default'
          headers: { 'Content-Type':'application/x-www-form-urlencoded'}
          # body:"pageNum=#{pageNum}&state=1"
        })
      .then (res)->
        res.json()
      .then (json)->
        date = new Date()
        dispatch actions.refreshListCompleted
          list:json
          lastFetchTime:date
      .catch (err)->
        console.log(err)


  fetchGetList:(pageNum,state)->
    (dispatch)->
      dispatch actions.getListStart()
      fetch("http://202.136.214.173/api/V_SHORT_OUTPLAN?state=#{state}&pageNum=#{pageNum}",{
          method: 'GET'
          mode: 'cors'
          cache: 'default'
          headers: { 'Content-Type':'application/x-www-form-urlencoded'}
          # body:"pageNum=#{pageNum}&state=1"
        })
      .then (res)->
        res.json()
      .then (json)->
        date = new Date()
        dispatch actions.getListCompleted
          list:json
          lastFetchTime:date
      .catch (err)->
        console.log(err)

module.exports = actions




# fetchGetList : (pageNum)->
#   (dispatch)->
#     # fetch('http://202.136.214.173/api/V_SHORT_INPLAN?pageNum=1&state=1'
#     dispatch(getInPlanListStart())
#     $.get 'http://202.136.214.173/api/V_SHORT_INATTEMP',{pageNum:pageNum,state:1},(data)->
#       if(pageNum is 1)
#         dispatch getRefreshList(data)
#       else
#         dispatch getInPlanList(data)
